.PHONY: all clean kernel drivers libraries programs tests generate

TOOLSDIR     := tools
TOOLSROOTDIR := root

PROJECT_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
export PATH  := $(PROJECT_ROOT)/$(TOOLSDIR)/$(TOOLSROOTDIR)/bin:$(PATH)

GENERATED_SOURCES := sdk/syscall_enum.h kernel/src/syscall_table.inl library/src/wrappers.c

all: livecd.iso tests

kernel: generate libraries
	$(MAKE) -C kernel all

drivers: kernel
	$(MAKE) -C drivers all

libraries: generate
	$(MAKE) -C libraries all

programs: libraries
	$(MAKE) -C programs all

tests:
	$(MAKE) -C tests all

generate: $(GENERATED_SOURCES)

sdk/syscall_enum.h: $(wildcard sdk/*.h) syscalls.awk
	@echo 'Generating system call sources/headers...'
	sort sdk/* | awk -v enum_file=sdk/syscall_enum.h -v table_file=kernel/src/syscall_table.inl -v wrappers_file=libraries/mlsys/src/wrappers.c -f syscalls.awk

kernel/src/syscall_table.inl: sdk/syscall_enum.h

library/src/wrappers.c: sdk/syscall_enum.h

livecd.iso : kernel drivers libraries programs
	mkdir -p livecd/monolithium livecd/boot/grub livecd/monolithium/drivers
	cp kernel/monolithium livecd/monolithium/kernel
	cp drivers/bin/* livecd/monolithium/drivers/
	cp programs/bin/* livecd/monolithium/
	cp grub.cfg livecd/boot/grub/grub.cfg
	grub-mkrescue -o $@ livecd

clean:
	$(MAKE) -C kernel clean
	$(MAKE) -C drivers clean
	$(MAKE) -C libraries clean
	$(MAKE) -C programs clean
	$(MAKE) -C tests clean
	rm -f $(GENERATED_SOURCES) livecd.iso
